using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace ApplicationLibrary.DataGridViewControls
{
    public class ComboboxDataGridViewControlColumn : DataGridViewComboBoxColumn
    {
        protected ComboBoxManager<string> cbbManager = null;

        public ComboboxDataGridViewControlColumn()
        {
        }

        virtual public void AttachDataReader(DataReaderUpdater dr, iColumn code, iColumn description)
        {
            AttachDataReader(dr, code, description, false);
        }

        virtual public void AttachDataReader(DataReaderUpdater dr, iColumn code, iColumn description, bool alsoNULL)
        {
            cbbManager = new ComboBoxManager<string>(this);
            if (GlobalInfo.DBaseInfo.dbManager != null)
            {
                if (alsoNULL)
                    cbbManager.AddValue(string.Empty, string.Empty);
                FillComboBox(dr, code, description);
                cbbManager.AttachTo(this);
                cbbManager.Refresh();
            }
        }

        virtual public void RefreshDataReader(DataReaderUpdater dr, iColumn code, iColumn description)
        {
            cbbManager.Clear();
            dr.Find();
            for (int t = 0; t < dr.Count; t++)
                cbbManager.AddValue(dr.GetValue<string>(code, t), dr.GetValue<string>(description, t));

            cbbManager.Refresh();
        }

        virtual protected void FillComboBox(DataReaderUpdater dr, iColumn code, iColumn description)
        {
            dr.Find();
            for (int t = 0; t < dr.Count; t++)
                cbbManager.AddValue(dr.GetValue<string>(code, t), dr.GetValue<string>(description, t));
        }

        virtual public string GetValue()
        {
            return cbbManager != null
                        ? cbbManager.GetValue()
                        : "";
        }
    }
}